﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ZHDJ.MobileInterface;
using ZHDJ.MobileInterface.Push;
using System.Text;
using SXT.Utility;

namespace ZHDJ.MobileInterface
{
    /// <summary>
    /// GovernmentServicesCloudSMSService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class GovernmentServicesCloudSMSService : System.Web.Services.WebService
    {       
        /// <summary>
        /// 政务云平台短信发送(post)
        /// </summary>
        /// <param name="conten">短信内容，不能超过1000个字符</param>
        /// <param name="mobiles">手机号码，多个手机号码以小写逗号分隔</param>
        /// <param name="validate">验证码（由接口提供）</param>
        /// <returns></returns>
        [WebMethod(Description = "党建调用政务云平台发送短信接口")]
        public string SMSSend(string conten, string mobiles, string validate)
        {
            if (validate != System.Configuration.ConfigurationManager.AppSettings["GovernmentServicesCloudSms.Validate"] || string.IsNullOrEmpty(mobiles))
            { return ""; }

            string retStr = string.Empty;
            try
            {
                //请求路径
                string requestUrl = System.Configuration.ConfigurationManager.AppSettings["GovernmentServicesCloudSms.InterfaceUrl"];
                //企业编码
                string spCodeStr = System.Configuration.ConfigurationManager.AppSettings["GovernmentServicesCloudSms.SpCode"];
                //用户名称
                string loginNameStr = System.Configuration.ConfigurationManager.AppSettings["GovernmentServicesCloudSms.LoginName"];
                //用户密码
                string passwordStr = System.Configuration.ConfigurationManager.AppSettings["GovernmentServicesCloudSms.Password"];
                //提交时检测方式1 --- 提交号码中有效的号码仍正常发出短信，无效的号码在返回参数faillist中列出,不为1 或该参数不存在 --- 提交号码中只要有无效的号码，
                //那么所有的号码都不发出短信，所有的号码在返回参数faillist中列出
                string checkingTypeStr = System.Configuration.ConfigurationManager.AppSettings["GovernmentServicesCloudSms.CheckingType"];


                ZHDJ.MobileInterface.Push.GovernmentServicesCloudSMSHelper.GovernmentServicesCloudSMSSendInParam inParam = new ZHDJ.MobileInterface.Push.GovernmentServicesCloudSMSHelper.GovernmentServicesCloudSMSSendInParam();
                inParam.SpCode = spCodeStr;
                inParam.LoginName = loginNameStr;
                inParam.Password = passwordStr;                
                //inParam.MessageContent = Encoding.GetEncoding("gb2312").GetBytes(conten).ToString();
                //inParam.MessageContent = Encoding.Default.GetString(Encoding.GetEncoding("gb2312").GetBytes(conten));
                inParam.MessageContent = Encoding.Default.GetString(Encoding.GetEncoding("gb2312").GetBytes(conten));
                inParam.UserNumber = mobiles;
                inParam.SerialNumber = string.Format("{0:yyyyMMddHHmmssffffff}", DateTime.Now);
                inParam.ScheduleTime = "";
                inParam.ExtendAccessNum = "";
                inParam.f = checkingTypeStr;

                //result返回的数据格式："result=0&description=错误描述&taskid=10130236875&faillist=无效号码字符串(多个用逗号隔开)"
                string result = GovernmentServicesCloudSMSHelper.SendMessage(requestUrl, inParam);
                if (string.IsNullOrEmpty(result) || result == "-1")
                {
                    retStr = string.Format(@"result={0}&description={1}&faillist={2}", "-1", "接口调用失败", "");
                }
                else
                {
                    string resultParam1 = result.Split('&')[0].ToString();//result=0
                    string resultParam2 = result.Split('&')[3].ToString();//faillist=无效号码字符串(多个用逗号隔开)
                    string resultCode = resultParam1.Substring(resultParam1.IndexOf("=") + 1);//截取返回的结果码
                    switch (resultCode)
                    {
                        case "0": retStr = string.Format(@"result={0}&description={1}&{2}", "0", "发送短信成功", resultParam2); break;
                        case "1": retStr = string.Format(@"result={0}&description={1}&{2}", "1", "提交参数不能为空", resultParam2); break;
                        case "2": retStr = string.Format(@"result={0}&description={1}&{2}", "2", "账号无效或权限不足", resultParam2); break;
                        case "3": retStr = string.Format(@"result={0}&description={1}&{2}", "3", "账号密码错误", resultParam2); break;
                        case "4": retStr = string.Format(@"result={0}&description={1}&{2}", "4", "预约发送时间格式不正确，应为yyyyMMddHHmmss", resultParam2); break;
                        case "5": retStr = string.Format(@"result={0}&description={1}&{2}", "5", "IP不合法", resultParam2); break;
                        case "6": retStr = string.Format(@"result={0}&description={1}&{2}", "6", "号码中含有无效号码或不在规定的号段或为免打扰号码", resultParam2); break;
                        case "7": retStr = string.Format(@"result={0}&description={1}&{2}", "7", "非法关键字", resultParam2); break;
                        case "8": retStr = string.Format(@"result={0}&description={1}&{2}", "8", "内容长度超过上限，最大402字符", resultParam2); break;

                        case "9": retStr = string.Format(@"result={0}&description={1}&{2}", "9", "接受号码过多，最大1000", resultParam2); break;
                        case "10": retStr = string.Format(@"result={0}&description={1}&{2}", "10", "黑名单用户", resultParam2); break;
                        case "11": retStr = string.Format(@"result={0}&description={1}&{2}", "11", "提交速度太快", resultParam2); break;
                        case "12": retStr = string.Format(@"result={0}&description={1}&{2}", "12", "您尚未订购[普通短信业务]，暂不能发送该类信息", resultParam2); break;
                        case "13": retStr = string.Format(@"result={0}&description={1}&{2}", "13", "您的[普通短信业务]剩余数量发送不足，暂不能发送该类信息", resultParam2); break;
                        case "14": retStr = string.Format(@"result={0}&description={1}&{2}", "14", "流水号格式不正确", resultParam2); break;
                        case "15": retStr = string.Format(@"result={0}&description={1}&{2}", "15", "流水号重复", resultParam2); break;
                        case "16": retStr = string.Format(@"result={0}&description={1}&{2}", "16", "超出发送上限（操作员帐户当日发送上限）", resultParam2); break;
                        case "17": retStr = string.Format(@"result={0}&description={1}&{2}", "17", "余额不足", resultParam2); break;

                        case "18": retStr = string.Format(@"result={0}&description={1}&{2}", "18", "扣费不成功", resultParam2); break;
                        case "20": retStr = string.Format(@"result={0}&description={1}&{2}", "20", "系统错误", resultParam2); break;
                        case "21": retStr = string.Format(@"result={0}&description={1}&{2}", "21", "密码错误次数达到5次", resultParam2); break;
                        case "24": retStr = string.Format(@"result={0}&description={1}&{2}", "24", "帐户状态不正常", resultParam2); break;
                        case "25": retStr = string.Format(@"result={0}&description={1}&{2}", "25", "账户权限不足", resultParam2); break;
                        case "26": retStr = string.Format(@"result={0}&description={1}&{2}", "26", "需要人工审核", resultParam2); break;
                        case "28": retStr = string.Format(@"result={0}&description={1}&{2}", "28", "发送内容与模板不符", resultParam2); break;
                        case "29": retStr = string.Format(@"result={0}&description={1}&{2}", "29", "扩展号太长或不是数字&accnum=", resultParam2); break;
                        case "32": retStr = string.Format(@"result={0}&description={1}&{2}", "32", "同一号码相同内容发送次数太多", resultParam2); break;
                        default: retStr = string.Format(@"result={0}&description={1}&{2}", resultCode, "该返回结果尚未定义", resultParam2); break;
                    }
                }

            }
            catch (Exception ex)
            {
                retStr = string.Format(@"result={0}&description={1}&faillist={2}", "-1", "接口调用失败", "");
                ExceptionLogs.Write(new Exception("调用党建平台(政务云)发短信接口时发生异常：" + ex.Message.ToString()));
            }

            return retStr;
        }


       
    }
}
